package exam

import (
	"github.com/gin-gonic/gin"
	"smartLearn/models"
	"smartLearn/modules/response"
	"smartLearn/public/common"
	"strconv"
	"strings"
)

func Add(c *gin.Context) {
	questionIds:=strings.Split(c.Query("ids"),",")
	questionIdsArr := make([]int, len(questionIds))

	var err error
	for i, v := range questionIds {
		questionIdsArr[i], err = strconv.Atoi(v)
		if err != nil {
			continue //如果传入非数组，那么忽略
		}
	}

	userId,err:=strconv.Atoi(c.Query("userId"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	subjectId,err:=strconv.Atoi(c.Query("subjectId"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}

	name:=c.Query("name")
	comment:=c.Query("comment")

	userExamModel:=models.UserExam{}
	userExamModel.UserId=userId
	userExamModel.Name=name
	userExamModel.Comment=comment
	userExamModel.SubjectId=subjectId
	userExamModel.Status=1
	if userExamModel.CheckName() {
		response.ShowError(c,"试卷名称已经存在")
		return
	}
	_,err=userExamModel.Add(questionIdsArr)
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	response.ShowSuccess(c,"组卷成功")
}
func RemoveQuestion(c *gin.Context) {
	examId,err:=strconv.Atoi(c.Query("examId"))
	if err != nil {
		response.ShowError(c, err.Error())
		return
	}
	questionId,err:=strconv.Atoi(c.Query("questionId"))
	if err != nil {
		response.ShowError(c, err.Error())
		return
	}
	userExamQuestionModel:=models.UserExamQuestion{}
	userExamQuestionModel.QuestionId=questionId
	userExamQuestionModel.ExamId=examId
	err= userExamQuestionModel.RemoveQuestionFromExam()
	if err!=nil {
		response.ShowError(c,err.Error())
	}
	response.ShowSuccess(c,"移除试题成功")
}
func ListQuestion(c *gin.Context) {
	examId,err:=strconv.Atoi(c.Query("examId"))
	if err != nil {
		response.ShowError(c, err.Error())
		return
	}
	userExamQuestionModel:=models.UserExamQuestion{}

	questionIdsArr,err:=userExamQuestionModel.GetByExamId(examId)
	if err != nil {
		response.ShowError(c, err.Error())
		return
	}

	questionIds:=""
	//把questionId拼接成字符串
	for i, v := range questionIdsArr {
		if i!=0 {
			questionIds+=","
		}
		questionIds+= strconv.Itoa(v)

	}

	var data = make(map[string]interface{}, 0)
	data["questionIds"]=questionIds
	response.ShowData(c,data)

}
func List(c *gin.Context) {

	pageNum, err := strconv.ParseInt(c.Query("pageNum"), 10, 64)
	if err != nil {
		response.ShowError(c, err.Error())
		return
	}

	pageSize, err := strconv.ParseInt(c.Query("pageSize"), 10, 64)
	if err != nil {
		response.ShowError(c, err.Error())
		return
	}
	paging := &common.Paging{Page: pageNum, PageSize: pageSize}
	userExam:=models.UserExam{}
	userExam.UserId, err = strconv.Atoi(c.Query("userId"))
	if err != nil {
		response.ShowError(c, err.Error())
		return
	}

	userExam.SubjectId, err = strconv.Atoi(c.Query("subjectId"))
	if err != nil {
		response.ShowError(c, err.Error())
		return
	}
	name:= c.Query("name")
	comment:= c.Query("comment")
	userExam.Name=name
	userExam.Comment=comment
	status:= c.Query("status")
	if status=="{}" ||  status==""  {
		userExam.Status=0 //表示所有的Status
	}else{
		userExam.Status,err = strconv.Atoi(status)
		if err != nil {
			response.ShowError(c, err.Error())
			return
		}
	}
	userExams,err:=userExam.List(paging)
	if err != nil {
		response.ShowError(c, err.Error())
		return
	}

	var data = make(map[string]interface{}, 0)
	data["total"]=paging.Total
	data["list"]=userExams
	response.ShowData(c,data)



}
func Delete(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		response.ShowError(c, err.Error())
		return
	}
	userExamModel:=models.UserExam{}
	userExamModel.Id=id
	err=userExamModel.DeleteById()
	if err != nil {
		response.ShowError(c, err.Error())
		return
	}
	response.ShowSuccess(c,"删除成功")
}
func Update(c *gin.Context) {

	id,err:=strconv.Atoi(c.Param("id"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	name:=c.Query("name")
	status,err:=strconv.Atoi(c.Query("status"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	comment:=c.Query("comment")


	userExamModel := models.UserExam{};
	userExamModel.Id=id
	userExamModel.Status=status
	userExamModel.Name=name
	userExamModel.Comment=comment


	newid,err:=userExamModel.Update()
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	var data = make(map[string]interface{}, 0)
	data["id"]=newid
	response.ShowData(c,data)

}